var p_lis = document.getElementsByClassName('p_lis');
var roll_abs = document.getElementsByClassName('roll_abs')[0];
var btn = document.getElementById('btn');
var rotate = document.getElementById('rotate');
var get_img = document.getElementById('get_img');
var get_name = document.getElementById('get_name');
var close = document.getElementById('close');
var btn_href = document.getElementById('btn_href');/**按钮跳转链接**/
var img_href = document.getElementById('img_href');/**图片跳转**/
var img_change = document.getElementById('img_change');/**改变图片**/
var btn = document.getElementById('btn');/**转盘按钮**/
var ip_time;/**苹果的定时器**/
var adr_time;/**安卓的定时器**/
var get_prize;/**弹出礼物的定时器**/
var run = false;/**判断开关**/
var is_rotate = false;/**转盘旋转开关**/
var turns = 0;/**旋转的次数**/
var change_h;/**弹出窗高度改变**/
var storage = window.localStorage;/**浏览器本地记录**/
var anco_id,aup_id;/**抽奖记录表总表id---抽奖记录明细表id**/
var url_id;/**活动id**/
var todatTime;

var p_w = 0;/**一个p的长度**/
var p_w_all;/**所有p的长度**/
for(var i=0; i<p_lis.length; i++){
	p_w = p_w + p_lis[i].scrollWidth;
	p_w_all = p_w;
}


/**用来保存奖品数据**/
var arr_id =[];
var arr_title = [];
var arr_imgurl = [];
var arr_link = [];
/**用来保存奖品数据**/


/**获取活动的id**/
var url_href = "https://collect.topeffects.cn/api_activity_info?activity_id=45";
function UrlSearch(){
    var name,value; 
    var str = url_href;
    console.log(str);
    var num = str.indexOf("?") 
    str = str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
    console.log(str)
    var arr = str.split("&"); //各个参数放到数组里
    console.log(arr);
    for(var i=0;i < arr.length;i++){ 
	    num = arr[i].indexOf("="); 
	    if(num > 0){ 
		    name = arr[i].substring(0,num);
		    value = arr[i].substr(num+1);
		    this[name] = value;
		    console.log("活动id是"+this[name]);
		    url_id = this[name];
		    } 
    } 
} 
UrlSearch() ;
/**获取活动的id**/





/**判断日期并且是否删除昨天的记录**/
function date(){
	var today = new Date();
	if(!storage.getItem(url_id+'today')){
		storage.setItem(url_id+'today',today.getDate());
	}else{
		var now_today = today.getDate();
		console.log("今天是"+now_today+"号");
		if(now_today > parseInt(storage.getItem(url_id+'today'))){
			storage.removeItem(url_id+'today');
			storage.removeItem(url_id+'add_times');
			storage.removeItem(url_id+'times');
			storage.removeItem(url_id+'furbish');
			storage.removeItem(url_id+'prize_title');
			storage.removeItem(url_id+'prize_url');
			storage.removeItem(url_id+'prize_id');
			storage.setItem(url_id+'today',now_today);
		}
	}
}
date();
/**判断日期并且是否删除昨天的记录**/


/**顺序抽奖的牌号**/
function add_times(){
	if(!storage.getItem(url_id+'add_times')){
		storage.setItem(url_id+'add_times',0);
	}
}
add_times();
/**顺序抽奖的牌号**/


/**判断是否安卓**/
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); 
/**判断是否安卓**/
if(isiOS == true  && isAndroid == false){
	marquee_ip();	
}else if(isAndroid == true && isiOS == false){
	marquee_adr();
}





/**剩余次数**/
function dayTimes(time){
	if(!storage.getItem(url_id+'times')){
		storage.setItem(url_id+'times',time);
	}
	var todayTimes = storage.getItem(url_id+'times');
	
	btn.addEventListener('click',function(){
		if(is_rotate === false){
			is_rotate = true;
			if(todayTimes > 0){
				click_display();
				img_change.src = 'images/click-after.png';/**改变点击图片**/
				storage.setItem(url_id+'times', parseInt(storage.getItem(url_id+"times")) - 1)
				todayTimes = storage.getItem(url_id+'times');
				document.getElementById('times').innerHTML = storage.getItem(url_id+'times');
			}else if(todayTimes <= 0){
				is_rotate = false;
				alert('今天次数用完了,请刷新页面重置次数');
				storage.setItem(url_id+'furbish',1);
			}
			if(parseInt(storage.getItem(url_id+'times')) <= 0){
				storage.setItem(url_id+'times', 0);
				document.getElementById('times').innerHTML = 0;
			}
		}
	});
	document.getElementById('times').innerHTML = parseInt(storage.getItem(url_id+'times'));/*必须两个同时存在，才可以显示*/
}
/**剩余次数**/







/**保存奖品ID，使用拼接的方法**/
function save_prize(prize_id){
	var new_list;
	if( storage.getItem(url_id+'prize_id') != null ){
	    new_list = storage.getItem(url_id+'prize_id') + ',' + prize_id;
	    storage.setItem(url_id+'prize_id', new_list);
	} else {
	    storage.setItem(url_id+'prize_id',prize_id);
	}
}
/**保存奖品ID，使用拼接的方法**/



/**保存奖品url**/
function save_url(prize_url){
	var new_url;
	if( storage.getItem(url_id+'prize_url') != null ){
	    new_url = storage.getItem(url_id+'prize_url') + ',' + prize_url;
	    storage.setItem(url_id+'prize_url', new_url);
	} else {
	    storage.setItem(url_id+'prize_url',prize_url);
	}
}
/**保存奖品url**/



/**保存奖品的文字**/
function save_title(prize_title){
	var new_title;
	if( storage.getItem(url_id+'prize_title') != null ){
	    new_title = storage.getItem(url_id+'prize_title') + ',' + prize_title;
	    storage.setItem(url_id+'prize_title', new_title);
	} else {
	    storage.setItem(url_id+'prize_title',prize_title);
	}
}
/**保存奖品的文字**/


/**奖品列表自动生成**/
function had_list(){
	var get_url,url_arr = 0;
	var get_title,title_arr = 0;
	if( storage.getItem(url_id+'prize_url') != null ){
		var get_url = storage.getItem(url_id+'prize_url');
		var url_arr = get_url.split(',');
	} 
	if( storage.getItem(url_id+'prize_title') != null ){
		var get_title = storage.getItem(url_id+'prize_title');
		var title_arr = get_title.split(',');
	} 
	if(url_arr.length>0){
		for(var i=0; i<url_arr.length; i++){
			var new_div = "<div class='history_item'>"
						 +"<div class='history_award'>"
						 +"<img class='new_img'/>"
						 +"</div>"
						 +"<div class='history_word'>"
						 +"<p>"+title_arr[i]+"</p>"
						 +"<p>有效期：<span>2017-07-31</span></p>"
						 +"</div>"
						 +"<div class='history_right'>&gt;</div>"
						 +"</div>";
			$('.history_items').append(new_div);
			$('.history_item .history_award img').eq(i).attr('src','https://collect.topeffects.cn/'+url_arr[i]);
		}
	}
}
/**奖品列表自动生成**/



/**获得奖品**/
function create_list(create_title,create_url){
	var create_div = "<div class='history_item'>"
				 +"<div class='history_award'>"
				 +"<img class='new_img'/>"
				 +"</div>"
				 +"<div class='history_word'>"
				 +"<p>"+create_title+"</p>"
				 +"<p>有效期：<span>2017-07-31</span></p>"
				 +"</div>"
				 +"<div class='history_right'>&gt;</div>"
				 +"</div>";
	$('.history_items').append(create_div);
	$('.new_img').eq($('.history_award').length-1).attr('src','https://collect.topeffects.cn/'+create_url);
}
/**获得奖品**/




/**把接口二数据返回给后台**/
function post_back(act_id,prize_id){
	$.ajax({
        type: "get",
        url: "https://collect.topeffects.cn/api_is_user_coupon_add",
        data: {'visitorid':"0",'activity_id':act_id,'couponid':prize_id},
        dataType: "json",
         
        success: function (data) {
           console.log('请求成功');
        },
        error: function (msg) {
            console.log(msg);
        }
   });
}
/**把接口二数据返回给后台**/


/**接口二获取曝光次数**/
function get_exposure(activity_id,couponid){
	$.ajax({
        type:"get",
        url:"https://collect.topeffects.cn/api_is_user_coupon_add?visitorid=0&activity_id="+activity_id+"&couponid="+couponid,
        dataType: "json",
         
        success: function(data){
        	console.log(data);
        	anco_id = data.anco_id;
            aup_id = data.aup_id;
            console.log(anco_id);
            console.log(aup_id);
        },
        error:function(msg){
        	console.log(msg);
        }
    });
}
/**接口二获取曝光次数**/



window.onload = function(){
	change_h = parseInt($('#img').css('height'));/**图片高度**/
	
	/*适应高度*/
	var win_h = parseInt(window.screen.height);
	var web_h = parseInt($('.web').css('height'));
	web_h <= win_h ? $('.web').css('height', '100%') : $('.web').css('height', web_h+'px') ;
	
	var history_h = parseInt($('.history').css('height'));
	history_h > win_h ? $('.history').css('height', history_h+'px') : $('.history').css('height', '100%') ;
	/*适应高度*/	
	
	
	/**弹出规则窗$$$我的奖品**/
	$('.rule').click(function(){
		$('.description_mask').css('display', 'block');
		$('.web').on('touchmove', function(e) {
		  e.preventDefault();
		}, false);
	});
	$('.prize').click(function(){
		$('.history').css('display', 'block');
		$('.dial').css('display', 'none');
	});
	$('.close_des').click(function(){
		$('.history').css('display', 'none');
		$('.description_mask').css('display', 'none');
		$('.web').css('display', 'block');
		$('.dial').css('display', 'block');
		$('.web').unbind('touchmove');
	});
	/**弹出规则窗$$$我的奖品**/	

	
	
	/**获取后台信息**/
	$(function(){
		$.ajax({
	        type:"GET",
	        url:"https://collect.topeffects.cn/api_activity_info?activity_id="+url_id,
	        dataType: "json",
	         
	        success: function(data){
	        	todatTime = data.coupon_count;
	        	console.log("一轮的次数是"+todatTime);
	        	dayTimes(todatTime);
	        	for(var k in data.coupon_list){
	        		arr_id.push(data.coupon_list[k].id);
	        		arr_title.push(data.coupon_list[k].title);
	        		arr_imgurl.push(data.coupon_list[k].imgurl);
	        		arr_link.push(data.coupon_list[k].link);
	        	}
	        	had_list();
	        },
	        error:function(){
	        	console.log('error');
	        }
	    });
	});
	/**获取后台信息**/
	
	
	
	furbish();
	/**重置次数**/
	if(Boolean(parseInt(storage.getItem(url_id+'furbish'))) === true){
		storage.setItem(url_id+'furbish',0);
		storage.setItem(url_id+'add_times',0);
		$(function(){
			$.ajax({
		        type:"GET",
		        url:"https://collect.topeffects.cn/api_activity_info?activity_id="+url_id,
		        dataType: "json",
		         
		        success: function(data){
		        	storage.setItem(url_id+'times',parseInt(data.coupon_count));
		        	document.getElementById('times').innerHTML = storage.getItem(url_id+'times');
		        },
		        error:function(){
		        	console.log('error');
		        }
		    });
		});
	}
	/**重置次数**/
}



/**提示竖屏**/
window.onresize = function(){
	function hengshuping() {
        if (window.orientation == 90 || window.orientation == -90) {
           alert('请在竖屏下使用');
        }
    }
	hengshuping();
}
/**提示竖屏**/





/**跑马灯**/
function marquee_ip(){
	var roll_w = document.getElementsByClassName('roll')[0].offsetWidth;/**roll的长度**/
	var offset_l = document.getElementsByClassName('roll')[0].offsetLeft;/**距离左边的高度**/
	var win_w = parseInt(document.body.offsetWidth);/**body的宽度**/
	if(run == false){
		clearTimeout(ip_time);
		ip_time = setTimeout("marquee_ip()", 17000);
		roll_abs.style.transform = 'translateX('+(-p_w_all - offset_l - win_w*0.55)+'px)';
		roll_abs.style.transition = 'transform 17s linear';
		run = true;
	}else if(run == true){
		clearTimeout(ip_time);
		ip_time = setTimeout("marquee_ip()", 10);
		roll_abs.style.transition = 'none';
		roll_abs.style.transform = 'translateX('+roll_w+'px)';
		run = false;
	}
}
function marquee_adr(){
	var roll_w = document.getElementsByClassName('roll')[0].offsetWidth;/**roll的长度**/
	var offset_l = document.getElementsByClassName('roll')[0].offsetLeft;/**距离左边的高度**/
	var win_w = parseInt(document.body.offsetWidth);/**body的宽度**/
	if(run == false){
		clearTimeout(adr_time);
		adr_time = setTimeout("marquee_adr()", 17000);
		roll_abs.style.transform = 'translateX('+(-p_w_all + win_w*0.48 + offset_l)+'px)';
		roll_abs.style.transition = 'transform 17s linear';
		run = true;
	}else if(run == true){
		clearTimeout(adr_time);
		adr_time = setTimeout("marquee_adr()", 10);
		roll_abs.style.transition = 'none';
		roll_abs.style.transform = 'translateX('+roll_w+'px)';
		run = false;
	}
}
/**跑马灯**/



/**点击旋转&&&&弹出奖品**/
function click_display(){
	var random_num = Math.random();/**转盘度数**/
	var opposite;/**转盘的加减**/
	turns ++;
	turns%2==0?opposite=1:opposite=-1;
	rotate_img.style.transform = 'rotate('+(2215+360*6*(turns-1)+opposite*10*random_num)+'deg)';
	rotate_img.style.transition = 'transform 4s ease-in-out';
	
	get_prize = setTimeout(function(){
		/**奖品显示**/
		$('.get_mask').css('display', 'block');
		
		/**安卓和苹果读取图片高度不一样**/
		if(isiOS == true  && isAndroid == false){
			change_h <= parseInt(window.screen.height) ? $('.get_mask').css('height', '100%') : $('.get_mask').css('height', change_h+'px') ;
		}else if(isAndroid == true && isiOS == false){
			$('.get_mask').css('height', '100%');
			$('.web').on('touchmove', function(e) {
				e.preventDefault();
			}, false);
		}
		/**安卓和苹果读取图片高度不一样**/
		
		/**顺序礼品**/
		
		get_img.src = 'https://collect.topeffects.cn/'+arr_imgurl[parseInt(storage.getItem(url_id+'add_times'))];
		get_name.innerHTML = arr_title[parseInt(storage.getItem(url_id+'add_times'))];
		btn_href.href = arr_link[parseInt(storage.getItem(url_id+'add_times'))];
		img_href.href = arr_link[parseInt(storage.getItem(url_id+'add_times'))];
		var prize_id = arr_id[parseInt(storage.getItem(url_id+'add_times'))];
		var prize_url = arr_imgurl[parseInt(storage.getItem(url_id+'add_times'))];
		var prize_title = arr_title[parseInt(storage.getItem(url_id+'add_times'))];
		
		storage.setItem(url_id+'add_times',parseInt(storage.getItem(url_id+'add_times'))+1);
		
		img_change.src = 'images/click-before.png';
		
		save_prize(prize_id);/**保存奖品ID**/
		save_url(prize_url);/**保存奖品url**/
		save_title(prize_title);/**保存奖品的文字**/
		create_list(prize_title,prize_url);/**获得奖品**/
		post_back(url_id,prize_id);/**把接口二数据返回给后台**/
		get_exposure(url_id,prize_id);/**接口二获取曝光次数**/
		
		/**接口三数据传到后台**/
		img_href.addEventListener('click',function(aup_id,anco_id,url_id,prize_id){
			$.ajax({
		        type: "get",
		        url: "https://collect.topeffects.cn/api_is_user_coupon_add",
		        data: {'prize_id':aup_id,'can_id':anco_id,'activity_id':url_id,'couponed':prize_id},
		        dataType: "json",
		         
		        success: function (data) {
		           console.log('请求成功');
		        },
		        error: function (msg) {
		            console.log(msg);
		        }
		   });
		});
		
	}, 4500);
}
/**点击旋转&&&&弹出奖品**/




/**奖品页隐藏**/
function click_none(){
	is_rotate = false;
	clearTimeout(get_prize);
	$('.get_mask').css('display', 'none');
	$('.web').unbind('touchmove');
}
function click_close(){
	close.addEventListener('click', click_none);
}
click_close();
/**奖品页隐藏**/



/**刷新次数**/
function furbish(){
	if(!storage.getItem(url_id+'furbish')){
		storage.setItem(url_id+'furbish',0);
	}
}
/**刷新次数**/





